package com.beyond.trace.service.impl;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beyond.trace.domain.TracePlantRecord;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.annotation.Resource;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import com.beyond.trace.mapper.TraceProductCertificateMapper;
import com.beyond.trace.domain.TraceProductCertificate;
import com.beyond.trace.service.ITraceProductCertificateService;

/**
 * 产品证书信息Service业务层处理
 * 
 * @author yao
 * @date 2022-03-02
 */
@Service
public class TraceProductCertificateServiceImpl extends ServiceImpl<TraceProductCertificateMapper, TraceProductCertificate> implements ITraceProductCertificateService
{
    @Resource
    private TraceProductCertificateMapper traceProductCertificateMapper;

    /**
     * 查询产品证书信息
     * 
     * @param certificateId 产品证书信息ID
     * @return 产品证书信息
     */
    @Override
    public TraceProductCertificate selectTraceProductCertificateById(Long certificateId)
    {
        QueryWrapper<TraceProductCertificate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("certificate_id",certificateId);
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return traceProductCertificateMapper.selectOne(queryWrapper);
    }

    /**
     * 查询产品证书信息列表
     * 
     * @param traceProductCertificate 产品证书信息
     * @return 产品证书信息
     */
    @Override
    public List<TraceProductCertificate> selectTraceProductCertificateList(TraceProductCertificate traceProductCertificate)
    {
        try{ //解决未传token的问题
            if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                traceProductCertificate.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return traceProductCertificateMapper.selectTraceProductCertificateList(traceProductCertificate);
    }

    /**
     * 新增产品证书信息
     * 
     * @param traceProductCertificate 产品证书信息
     * @return 结果
     */
    @Override
    public boolean insertTraceProductCertificate(TraceProductCertificate traceProductCertificate)
    {
        traceProductCertificate.setCreateTime(DateUtils.getNowDate());
        traceProductCertificate.setUpdateTime(DateUtils.getNowDate());
        traceProductCertificate.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        traceProductCertificate.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        traceProductCertificate.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
        //return traceProductCertificateMapper.insertTraceProductCertificate(traceProductCertificate);
        return super.save(traceProductCertificate);
    }

    /**
     * 修改产品证书信息
     * 
     * @param traceProductCertificate 产品证书信息
     * @return 结果
     */
    @Override
    public int updateTraceProductCertificate(TraceProductCertificate traceProductCertificate)
    {
        traceProductCertificate.setUpdateTime(DateUtils.getNowDate());
        traceProductCertificate.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return traceProductCertificateMapper.updateById(traceProductCertificate);
    }

    /**
     * 批量删除产品证书信息
     * 
     * @param certificateIds 需要删除的产品证书信息ID
     * @return 结果
     */
    @Override
    public int deleteTraceProductCertificateByIds(Long[] certificateIds)
    {
        QueryWrapper<TraceProductCertificate> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("certificate_id", Arrays.asList(certificateIds));
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return traceProductCertificateMapper.delete(queryWrapper);
    }

    /**
     * 删除产品证书信息信息
     * 
     * @param certificateId 产品证书信息ID
     * @return 结果
     */
    @Override
    public int deleteTraceProductCertificateById(Long certificateId)
    {
        QueryWrapper<TraceProductCertificate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("certificate_id", certificateId);
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return traceProductCertificateMapper.delete(queryWrapper);
    }
}
